

def dict_to_json(dictionary):
    import json
    return json.dumps(dictionary, sort_keys=True,
                     indent=4, separators=(',', ': '))


def harvest_txt(txt_file):
    import re
    data = {}
    for line in txt_file:
        line = re.sub('[!\,;\'\\\.\(\)]', ' ', line)
        line = re.sub('#[0-9A-Z]*(-[0-9A-Z])*', ' ', line)
#        line = re.sub('([^A-Za-z])[\'\"]([A-Za-z])', '$1 $2', line)
#        line = re.sub('([A-Za-z])[\'\"]([^A-Za-z])', '$1 $2', line)
        for word in line.split():
            if word in data:
                data[word] = data[word] + 1
            else:
                data[word] = 1
    return dict_to_json(data)


def harvest_dummy():
    data = {
            'who': 5,
            'was': 7
            }
    return dict_to_json(data)


def main():
    import argparse
    import sys
    parser = argparse.ArgumentParser(description='harvest words for'
                                     ' building dictionary')
    parser.add_argument('--in_file',
                        action='store',
                        nargs='?',
                        type=argparse.FileType('r'),
                        default=sys.stdin)
    parser.add_argument('--out_file',
                        action='store',
                        nargs='?',
                        type=argparse.FileType('w'),
                        default=sys.stdout)
    args = parser.parse_args()
    if args.in_file == sys.stdin:
        print harvest_dummy()
        args.out_file.write(harvest_dummy())
    else:
        args.out_file.write(harvest_txt(args.in_file))

if __name__ == "__main__":
    main()
